package org.pinda.network.browser;

import java.io.DataOutputStream;
import java.io.IOException;

import org.pinda.network.util.Stream;
import org.pinda.network.PacketUtils;
import org.pinda.network.ServerInfo;

/**
 * Adds a server to the server browser
 * @author Johnnei
 *
 */
public class PacketHostServer extends IBrowserPacket {
	
	private ServerInfo server;
	
	public PacketHostServer() {
		
	}
	
	public PacketHostServer(ServerInfo server) {
		this.server = server;
	}

	@Override
	public void write(DataOutputStream outStream) throws IOException {
		outStream.write(server.getIP());
		outStream.writeShort(server.getPort());
		PacketUtils.writeString(outStream, server.getRoomName());
		outStream.writeByte(server.getMaxPlayerCount());
	}

	@Override
	public void read(Stream inStream) throws IOException {
		byte[] ip = inStream.readBytes(4);
		short port = (short)inStream.readShort();
		String roomName = PacketUtils.readString(inStream);
		byte maxPlayerCount = (byte)inStream.readByte();
		server = new ServerInfo(ip, port, roomName, 1, maxPlayerCount);
	}

	@Override
	public short getId() {
		return 4;
	}

	@Override
	public int getLength() {
		return server.getLength() - 1;
	}

}
